package cn.edu.njuit.lesson9heros.utils;

import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;

/**
 * 文件工具类，通过包名获取文件路径
 */
public class FileTool {
    /**
     * 根据包名获得路径，递归写入components
     * @param packageName
     * @return
     */
    public static String packageFilePath(String packageName){
        String path=packageName.replace(".","/");
        try {
            //根据包名获取运行路径
            Enumeration<URL> urls=Thread.currentThread().getContextClassLoader().getResources(path);
            String filePath="";
            while(urls.hasMoreElements()){
                URL nextPath=urls.nextElement();
                filePath=nextPath.getFile();
                System.out.println(filePath);
                break;
            }
            return filePath;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
